<?php
include_once 'function.php';
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of login
 *
 * @author ivan
 */
class index {
    //put your code here
    
    private $email;
    private $password;
    private $Db;
    private $userType;
  

    public function __construct($email,$password){
        
        $email = htmlspecialchars($email);
        $password = htmlspecialchars($password);
        
        
        $this->email = $email;
        $this->password = $password;
        $this->Db = Db();
    }
    
    public function verify(){
        
        $pattern = "#^[\w.-]{1,64}@(spsu)\.(edu)$#";
        
        if(preg_match($pattern, $this->email)){
            
            return "ok";
        }
        else{
            
            $error = "Invalid email format";
             return $error;
        }
    }
    
    public function checkexistance(){
        
        $query = $this->Db->prepare('SELECT * FROM user where email = :email');
        $query->execute(array(
            'email' => $this->email
        ));
        
        $response = $query->fetch();
        
        if ($response == TRUE){
            
            $valid = $this->checkvalidity();
            return $valid;
        }
        
        else{
            
            $error = "Sorry, you are not registered";
            return $error;
        }
        
    }
    
    public function checkvalidity(){
        
        $query = $this->Db->prepare('SELECT * FROM user where email = :email AND password = :password');
        
        $query->execute(array(
            'password' => $this->password,
            'email' => $this->email
        ));
        
        $response = $query->fetch();
        
       
        
       
        
        if ($response == TRUE){
            
            if($response['emailVerify'] != "t"){
                
               $error = "Your email has not been verified";
               return $error;
            }
            if($response['isBanned'] != "f"){
                
               $error = "You have been banned";
               return $error;
            }
           
            return "ok";
            
        }
        else{
            
            $error = "Wrong password";
            return $error;
        }
        
        
        
    }
    
    
    
    public function userType(){
        
         $query = $this->Db->prepare('SELECT * FROM user where email = :email AND password = :password');
         $query->execute(array(
            'password' => $this->password,
            'email' => $this->email
        ));
        
        $response = $query->fetch();
        
         if ($response == TRUE){
            
            if($response['role'] != "admin"){
               
                return 1;
                
            }
            else{
                return 0;
            }
         }
        
        
    }
    
    
    public function session(){
 
        $query = $this->Db->prepare('SELECT * FROM user where email = :email');
        $query->execute(array('email'=> $this->email));
        $query = $query->fetch();
        $_SESSION['id'] = $query['id'];
        $_SESSION['username'] = $query['username'];
        
        
        $this->userType = $this->userType();
        
        if($this->userType==1){
            return 1;
        }
        else{
            return 2;
        }
    }
    
    
}
